package com.ys.malini.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.ys.malini.dao.IClassTypeDao;
import com.ys.malini.dao.ICommodityDao;
import com.ys.malini.dao.IImagesDao;
import com.ys.malini.model.TClasstype;
import com.ys.malini.model.TImages;
import com.ys.malini.model.TProduct;
import com.ys.malini.model.TProductDetail;
import com.ys.malini.model.TUser;
import com.ys.malini.util.DateUtil;
import com.ys.malini.util.PictureUpLoad;
import com.ys.malini.web.form.UploadForm;

@SuppressWarnings("unchecked")
public class CommodityAction extends DispatchAction {

	/**
	 *  商品操作类
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws java.lang.Exception
	 */
	ICommodityDao commodityDao;
	IImagesDao imagesDao;
	IClassTypeDao classTypeDao;
	
	public void setImagesDao(IImagesDao imagesDao) {
		this.imagesDao = imagesDao;
	}

	public void setCommodityDao(ICommodityDao commodityDao) {
		this.commodityDao = commodityDao;
	}

	public void setClassTypeDao(IClassTypeDao classTypeDao) {
		this.classTypeDao = classTypeDao;
	}

	public CommodityAction() {
		super();
	}
	
	public ActionForward saveProduct(ActionMapping mapping, ActionForm form,HttpServletRequest request,
			HttpServletResponse response)throws Exception {
		UploadForm updForm = (UploadForm) form;
//		TUser user = (TUser) request.getSession().getAttribute("ADMIN_USER");
		String hidvalue = request.getParameter("hidvalue");
		String productName = request.getParameter("productName");
		String color = request.getParameter("color");
		String brand = request.getParameter("brand");
		String stockNum = request.getParameter("stockNum");
		String title = request.getParameter("title");
		String series = request.getParameter("series");
		String material = request.getParameter("material");
		String productStatus = request.getParameter("productStatus");
		String isNew = request.getParameter("isNew");
		String productType = request.getParameter("productType");
		String bigClassType = request.getParameter("bigClassType");
		String midClassType = request.getParameter("midClassType");
		String smaClassType = request.getParameter("smaClassType");
		String productCode = request.getParameter("productCode");
		String productBriefCode = request.getParameter("productBriefCode");
		String productBarCode = request.getParameter("productBarCode");
		String areaCode = request.getParameter("areaCode");
		String rank = request.getParameter("rank");
		String size = request.getParameter("size");
		String specCode = request.getParameter("specCode");
		String costPrice = request.getParameter("costPrice");
		String marketPrice = request.getParameter("marketPrice");
		String salePrice = request.getParameter("salePrice");
		String barginPrice = request.getParameter("barginPrice");
		String saleDate = request.getParameter("saleDate");
		String produceDate = request.getParameter("produceDate");
		String productInfo = request.getParameter("productInfo");
		String parameterInfo = request.getParameter("parameterInfo");
		String packInfo = request.getParameter("packInfo");
		String otherInfo = request.getParameter("otherInfo");
		String createTime = DateUtil.getCurrentLongDate();
		String createUser = "wy";
		TProduct product = new TProduct(productName, productType,isNew,productStatus, color, brand, title,
				series, material, bigClassType,midClassType, smaClassType);
		product = commodityDao.saveProduct(product);
		int productId = product.getProductId();
		TProductDetail productDetail = new TProductDetail(productId,isNew,productCode,
				productBarCode, productBriefCode, brand, title, series, material, areaCode, saleDate, rank,
				createTime, createUser,  productInfo, parameterInfo,packInfo, otherInfo, Double.valueOf(marketPrice),
				Double.valueOf(salePrice), Double.valueOf(barginPrice), Double.valueOf(costPrice),
				Integer.parseInt(stockNum), produceDate,size, specCode);
		commodityDao.saveProductDetail(productDetail);
		String fileName = PictureUpLoad.fileUpLoad(updForm, request,response,bigClassType,midClassType,smaClassType );
		String[] fileArr1 = fileName.split("#");
		for (int i = 0; i < fileArr1.length; i++) {
			String[] fileArr2 = fileArr1[i].split("@");
//			for (int j = 0; j < fileArr2.length; j++) {
//				System.out.println("filename"+j+"=" + fileArr2[j]);
//			}
			String mainproductColor = request.getParameter("mainColor" + i);
			TImages mainimages = new TImages(product.getProductId(), mainproductColor, fileArr2[1],fileArr2[0], "", "1");
			imagesDao.saveImages(mainimages);
			
		}
		
		
		for (int i = 0; i < fileArr1.length; i++) {
			String[] fileArr2 = fileArr1[i].split("@");
//			for (int j = 0; j < fileArr2.length; j++) {
//				System.out.println("filename"+j+"=" + fileArr2[j]);
//			}
	
			String productColor = request.getParameter("productColor" + i);
			TImages images = new TImages(product.getProductId(), productColor, fileArr2[1],fileArr2[0], "", "1");
			imagesDao.saveImages(images);
		}
		
		
		
		if(!"".equals(hidvalue)&&hidvalue!=null){
			String[] splitArr = hidvalue.split("|");
			for(int i=0;i<splitArr.length;i++){
				commodityDao.updateSubProduct(splitArr[i],String.valueOf(productId));
			}
		}
		request.setAttribute("uploadMsg", "产品上传成功");
		return mapping.findForward("success");
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form,HttpServletRequest request,
			HttpServletResponse response)throws Exception {
		TUser user = (TUser) request.getSession().getAttribute("ADMIN_USER");
		String productId = request.getParameter("productId");
		String productName = request.getParameter("productName");
		String color = request.getParameter("color");
		String brand = request.getParameter("brand");
		String stockNum = request.getParameter("stockNum");
		String title = request.getParameter("title");
		String series = request.getParameter("series");
		String material = request.getParameter("material");
		String productStatus = request.getParameter("productStatus");
		String isNew = request.getParameter("isNew");
		String productType = request.getParameter("productType");
		String bigClassType = request.getParameter("bigClassType");
		String midClassType = request.getParameter("midClassType");
		String smaClassType = request.getParameter("smaClassType");
		String productCode = request.getParameter("productCode");
		String productBriefCode = request.getParameter("productBriefCode");
		String productBarCode = request.getParameter("productBarCode");
		String areaCode = request.getParameter("areaCode");
		String rank = request.getParameter("rank");
		String size = request.getParameter("size");
		String specCode = request.getParameter("specCode");
		String costPrice = request.getParameter("costPrice");
		String marketPrice = request.getParameter("marketPrice");
		String salePrice = request.getParameter("salePrice");
		String barginPrice = request.getParameter("barginPrice");
		String saleDate = request.getParameter("saleDate");
		String produceDate = request.getParameter("produceDate");
		String productInfo = request.getParameter("productInfo");
		String parameterInfo = request.getParameter("parameterInfo");
		String packInfo = request.getParameter("packInfo");
		String otherInfo = request.getParameter("otherInfo");
		String createTime = DateUtil.getCurrentLongDate();
		String createUser = "wy";
		TProduct product = new TProduct(productName, productType,isNew,productStatus, color, brand, title,
				series, material, bigClassType,midClassType, smaClassType);
		product.setProductId(Integer.parseInt(productId));
		product = commodityDao.saveProduct(product);
		TProductDetail productDetail = new TProductDetail(Integer.parseInt(productId),isNew,productCode,
				productBarCode, productBriefCode, brand, title, series, material, areaCode, saleDate, rank,
				createTime, createUser,  productInfo, parameterInfo,packInfo, otherInfo, Double.valueOf(marketPrice),
				Double.valueOf(salePrice), Double.valueOf(barginPrice), Double.valueOf(costPrice),
				Integer.parseInt(stockNum), produceDate,size, specCode);
		commodityDao.saveProductDetail(productDetail);
		
		request.setAttribute("listMsg", "更新成功");
		listCommodity(mapping,form,request,response);
		return mapping.findForward("listCommodity");
	}
	
	public ActionForward listCommodity(ActionMapping mapping, ActionForm form,HttpServletRequest request,
			HttpServletResponse response)throws Exception {
		List commodityList = commodityDao.findAllProducts();
		request.setAttribute("commodityList", commodityList);
		if(commodityList.size()==0){
			request.setAttribute("listMsg", "没有商品");
		}
		return mapping.findForward("listCommodity");
	}
	
	public ActionForward deleteCommodity(ActionMapping mapping, ActionForm form,HttpServletRequest request,
			HttpServletResponse response)throws Exception {
		TUser user = (TUser) request.getSession().getAttribute("ADMIN_USER");
		String productId = request.getParameter("productId");
		commodityDao.removeCommodityById(productId);
		request.setAttribute("listMsg", "删除成功");
		listCommodity(mapping,form,request,response);
		return mapping.findForward("listCommodity");
	}
	
	public ActionForward updateCommodity(ActionMapping mapping, ActionForm form,HttpServletRequest request,
			HttpServletResponse response)throws Exception {
		TUser user = (TUser) request.getSession().getAttribute("ADMIN_USER");
		String productId = request.getParameter("productId");
		List<TClasstype> classList = classTypeDao.findPclassType();
		List list = commodityDao.findCommodityById(productId);
		request.setAttribute("classList", classList);
		request.setAttribute("list", list);
		return mapping.findForward("updateCommodity");
	}
	
	public ActionForward viewCommodity(ActionMapping mapping, ActionForm form,HttpServletRequest request,
			HttpServletResponse response)throws Exception {
		TUser user = (TUser) request.getSession().getAttribute("ADMIN_USER");
		String productId = request.getParameter("productId");
		TProductDetail product = commodityDao.findProductById(productId);
		request.setAttribute("product", product);
		return mapping.findForward("viewCommodity");
	}
	
	public ActionForward addCommodity(ActionMapping mapping, ActionForm form,HttpServletRequest request,
			HttpServletResponse response)throws Exception {
		TUser user = (TUser) request.getSession().getAttribute("ADMIN_USER");
		List<TClasstype> classList = classTypeDao.findPclassType();
		request.setAttribute("classList", classList);
		return mapping.findForward("add");
	}
	public ActionForward queryDiv(ActionMapping mapping, ActionForm form,HttpServletRequest request,
			HttpServletResponse response)throws Exception {
		TUser user = (TUser) request.getSession().getAttribute("ADMIN_USER");
		String bigClass= request.getParameter("bigClass");
	//	String midClass= request.getParameter("midClass");
		
		List divList = commodityDao.queryCommodityDes(bigClass);
		request.setAttribute("divList", divList);
		return mapping.findForward("showDiv");
	}
}
